package com.css.apps.base.common.module;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.model.SDict;
import com.css.apps.base.dict.service.QueryDict;
import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.menu.model.SMenu;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.role.model.SRole;
import com.css.common.plugins.IDisposable;
import com.css.common.plugins.Initializable;
import com.css.common.plugins.impl.AbstractConfigurablePlugin;
import com.css.core.model.tree.TreeCache;
import com.css.db.query.QueryCache;

public class CacheLoader extends AbstractConfigurablePlugin implements IDisposable, Initializable {
	
	private static Log log = LogFactory.getLog(CacheLoader.class);

	@Override
	protected void doConfig(Map<String, String> arg0) {

	}

	@Override
	public void dispose() {
		com.css.db.memcached.MemCachedFactory.buildClient().flushAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void initialize() {
		// 加载字典
		List<String> lst = new QueryDict("select uuid from SDict").list();
		QueryDict.idToObj(SDict.class, lst);
		log.info("已加载数据字典!");
		// 加载菜单树
		new TreeCache(SMenu.class).getTree();
		// lst = new QueryCache("select menuId from SMenu").list();
		// QueryCache.idToObj(SMenu.class, lst);
		log.info("已加载系统菜单!");
		// 加载功能树
		new TreeCache(SFunc.class).getTree();
		// lst = new QueryCache("select uuid from SFunc").list();
		// QueryCache.idToObj(SFunc.class, lst);
		log.info("已加载系统功能!");
		// 加载机构树
		new TreeCache(SOrg.class).getTree();
		// 加载机构
		// lst = new QueryCache("select uuid from SOrg").list();
		// QueryCache.idToObj(SOrg.class, lst);
		log.info("已加载系统机构!");
		// 加载角色
		lst = new QueryCache("select uuid from SRole").list();
		QueryCache.idToObj(SRole.class, lst);
		log.info("已加载系统角色!");
		// 加载岗位
		lst = new QueryCache("select uuid from SPost").list();
		QueryCache.idToObj(SPost.class, lst);
		log.info("已加载系统岗位!");
	}
}
